<?php

abstract class Registry{
    abstract  protected function get($key);
    abstract protected function set($key,$val);
}

//请求注册表
class RequestRegistry extends Registry{
    private $values = array();
    private static $instance;

    private function __construct()
    {
    }

    static function instance(){
        if(!isset(self::$instance)){
            self::$instance = new self();
        }
        return self::$instance;
    }

    protected function get($key)
    {
        if(isset($this->values[$key])){
            return $this->values[$key];
        }
        return null;
    }

    protected function set($key, $val)
    {
        $this->values[$key] = $val;
    }

    static function getRequest(){
        return self::$instance->get('request');
    }

    static function setRequest(Request $request){
        self::$instance->set('request',$request);
    }
}

class Request{
    private $properties;
    private $feedback = array();

    function __construct()
    {
        $this->init();
        RequestRegistry::setRequest($this);
    }

    function init(){
        if(isset($_SERVER['REQUEST_METHOD'])){
            $this->properties = $_REQUEST;
            return ;
        }

        foreach($_SERVER['argv'] as $arg){
            if(strpos($arg,'=')){
                list($key,$val) = explode('=',$arg);
                $this->setProperty($key,$val);
            }
        }
    }

    function getProperty($key){
        if(isset($this->properties[$key])){
            return $this->properties[$key];
        }
    }

    function setProperty($key,$val){
        $this->properties[$key] = $val;
    }

    function addFeedBack($msg){
        array_push($this->feedback,$msg);
    }

    function getFeedBack(){
        return $this->feedback;
    }

    function getFeedBackString($separator = '\n'){
        return implode($separator,$this->feedback);
    }
}

abstract class PageController{
    private $request;
    function __construct()
    {
        $request = RequestRegistry::getRequest();
        if(is_null($request)){
            $request  = new Request();
        }
        $this->request = $request;
    }

    abstract function process();

    function forward($resource){
        include($resource);
        exit(0);
    }

    function getRequest(){
        return $this->request;
    }
}

class AddVenueController extends PageController{
    function process()
    {
        try{
            $request = $this->getRequest();
            $name = $request->getProperty('venue_name');
            if(is_null($request->getProperty('submitted'))){
                $request->addFeedBack('为场地选择一个名字');
                $this->forward('add_venue.php');
            }else if(is_null($name)){
                $request->addFeedBack('名字必填');
                $this->forward('add_venue.php');
            }

            //将数据添加到数据库
            $venue = new Venue(null,$name);
            //加载列表页面
            $this->forward('ListVenue.php');
        }catch(Exception $e){
            $this->forward('error.php');
        }
    }
}